package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.BindCardRequest;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.mvp.contract.IUserAddNewCardContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 14:00
 * description:
 */
public class UserAddNewsCardPresenter extends IBasePresenter
        implements IUserAddNewCardContact.IUserAddNewCardPresenter<IUserAddNewCardContact.IUserAddNewCardView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public UserAddNewsCardPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void getUserInfo() {

    }

    @Override
    public void bindCard(String etCard, String etNick, String card_nick_name, String card_no) {
        BindCardRequest bindCardRequest = new BindCardRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        bindCardRequest.setAccess_token(access_token);
        bindCardRequest.setCard_nick_name(etNick);
        bindCardRequest.setCard_no(etCard);
        bindCardRequest.setPaper_no(card_no);
        bindCardRequest.setUser_name(card_nick_name);
        bindCardRequest.setTrade_type("00");
        String json = new Gson().toJson(bindCardRequest);
        mNetworkDateSource.bindCard(bindCardRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                if (mView != null) {
                    ((IUserAddNewCardContact.IUserAddNewCardView) mView).bindSuccess();
                }

            }

            @Override
            protected void onNext(IbaseBean response) {
                ((IUserAddNewCardContact.IUserAddNewCardView) mView).bindFaile(response.getMessage());
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                if (response.getCode().equals("U000003")){
                    ((IUserAddNewCardContact.IUserAddNewCardView) mView).bindHasAcard(response.getMessage());
                }else {
                    ((IUserAddNewCardContact.IUserAddNewCardView) mView).bindFaile(response.getMessage());
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IUserAddNewCardContact.IUserAddNewCardView) mView).bindFaile(throwable.getMessage());
            }
        });
    }

    private void getToken() {


    }

}
